<?php
class customer extends CI_Controller{
	private $data;
	public function __construct(){
		parent::__construct();
		if(!Auth::isLoggedIn()) redirect("/");
		$this->load->model("model_customer");
	}
		
	/*
			=================Function Add Customer=================
	*/
	public function index($set="sot"){
		$data['addCustomer'] = 'y';
		$data['masterinput'] = 'y';
		
		$data['title'] = TITLE.' - Tambah Data Pelanggan';
		if($this->input->post("btnSave") ){
			
			$hasil = $this->_addCustomer();
			if($hasil) redirect("/customer/index/set");;
			
		}
		if($set=="set"){
			$data['success'] = "Pelanggan Berhasil Ditambahkan";
		}
		else if($set=="err"){
			$data['success'] = "Error, harus dipilih salah satu";
		}
		$this->load->view("customerAdd",$data);
	}
	
	
	
	function _addCustomer(){
		$this->form_validation->set_message('_checkCustAva', 
							'Data sudah pernah diinputkan');
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               	array(
                     'field'   => 'nama',
                     'label'   => 'Nama',
                     'rules'   => 'trim|required|callback__checkCustAva['
					 										.$this->input->post('no_telp').']'
                  ),
			 	array(
                     'field'   => 'no_telp',
                     'label'   => 'Telepon',
                     'rules'   => 'trim|xss_clean|required'									
                  ),
				array(
                     'field'   => 'alamat',
                     'label'   => 'Alamat',
                     'rules'   => 'trim|required|xss_clean'
                  ),
				  array(
                     'field'   => 'alamat_1',
                     'label'   => 'Alamat',
                     'rules'   => 'trim|required|xss_clean'
                  ),
				  array(
                     'field'   => 'alamat_2',
                     'label'   => 'Alamat',
                     'rules'   => 'trim|required|xss_clean'
                  ),
               	array(
                     'field'   => 'npwp',
                     'label'   => 'npwp',
                     'rules'   => 'trim|xss_clean'
                  ),
				  array(
                     'field'   => 'perusahaan',
                     'label'   => 'perusahaan',
                     'rules'   => 'trim|xss_clean'
                  ),
				  	array(
                     'field'   => 'fax',
                     'label'   => 'fax',
                     'rules'   => 'trim|xss_clean'
                  ),
				  array(
                     'field'   => 'kode_pos',
                     'label'   => 'kode_pos',
                     'rules'   => 'trim|xss_clean'
                  ),
				  array(
                     'field'   => 'kota',
                     'label'   => 'Kota',
                     'rules'   => 'trim|required'
                  ),
				 array(
                     'field'   => 'provinsi',
                     'label'   => 'Provinsi',
                     'rules'   => 'trim|required'
                  )
				
            );
		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			$this->model_customer->addCustomer();
			return true;
		}
	}
	
	/*
			=================Function View Customer=================
	*/
	public function viewcustomer(){
		$data['masterview'] = 'y';
		$data['viewCustomer'] = 'y';
		$data['title'] = TITLE.' - Lihat Daftar Customer'; 
		$data['perusahaan'] = $this->model_customer->getPtList();
		$data['customer'] = $this->model_customer->getCustomerList(); 
		$this->load->view("customerview",$data);
	}
	
	function ajaxLoadCustList(){
		$ptId = $_POST['perusahaan'];
		$mode = $_POST['mymode'];
		$data['debug'] = $ptId = $_POST['perusahaan'];
		$data['debug1'] =$ptId;
		$data['customer'] = $this->model_customer->ajaxGetCustomerList($ptId, $mode);
		$this->load->view("custList", $data);
	}
	
	/*
			=================Function Edit Customer=================
	*/
	
	public function edit(){
		$cuId = $this->uri->segment(3, 0);
		$sFlag =  $this->uri->segment(4, 0);
		$data['customerdet'] = $this->model_customer->getCustomerDet($cuId); 
		$data['masterview'] = 'y';
		$data['viewCustomer'] = 'y';
		$data['custId'] = $cuId;
		$data['title'] = TITLE.' - Ubah Data Pelanggan';
		if($this->input->post("btnSave")){
			$hasil = $this->_editCustomer($cuId);
			if($hasil) {
				$data['success'] = "Data Berhasil Di Ubah";
				redirect('/customer/edit/' .$cuId .'/s', 'refresh');
			}
		}
		if($sFlag!=""){
			$data['success'] = "Data Berhasil Di Ubah";
		}
		$this->load->view("customeredit",$data);
	}
	
	function _editCustomer($custoId){
		$this->form_validation->set_error_delimiters('', '');
		$config = array(
               	array(
                     'field'   => 'nama',
                     'label'   => 'Nama',
                     'rules'   => 'trim|required'
                  ),
			 	array(
                     'field'   => 'no_telp',
                     'label'   => 'Telepon',
                     'rules'   => 'trim|xss_clean|required'									
                  ),
				array(
                     'field'   => 'alamat',
                     'label'   => 'Alamat',
                     'rules'   => 'trim|required|xss_clean'
                  ),
               	array(
                     'field'   => 'kota',
                     'label'   => 'Kota',
                     'rules'   => 'trim|required'
                  ),
				 array(
                     'field'   => 'provinsi',
                     'label'   => 'Provinsi',
                     'rules'   => 'trim|required'
                  )
				
            );
		$this->form_validation->set_rules($config); 
		if ($this->form_validation->run() == FALSE){
			return false;
		}
		else{
			$this->model_customer->editCustomer($custoId);
			return true;
		}
	}
	/*
			=================Function Delete Customer============= ====
	*/
	function deleteCustomer(){
		$idCust = $this->uri->segment(3, 0);
		$this->model_customer->delDataCust($idCust);
		redirect('/customer/viewcustomer');
	}
	
	
	
	/*
			=================Multi Purpose Function=================
	*/
	function _checkCustAva($namaCust, $noTelp){
		$benar = 0;
		$pt = $this->input->post('perusahaan');
		$data = $this->model_customer->getDataCust($namaCust, $noTelp, $pt);
		if(!$data){
			return true	;
		}else{
			return false;
		}
	}
}